﻿using System;
using System.IO;

namespace JESAI.DistributedLock.FileSystem;

/// <summary>
/// 实现<see cref="IDistributedLockProvider"/>以实现<see cref="FileDistributedLock"/>
/// </summary>
public sealed class FileDistributedSynchronizationProvider : IDistributedLockProvider
{
    private readonly DirectoryInfo _lockFileDirectory;

    /// <summary>
    /// 构造一个提供程序，在提供的范围内限定锁文件的作用域 <paramref name="lockFileDirectory"/>.
    /// </summary>
    public FileDistributedSynchronizationProvider(DirectoryInfo lockFileDirectory)
    {
        this._lockFileDirectory = lockFileDirectory ?? throw new ArgumentNullException(nameof(lockFileDirectory));
    }

    /// <summary>
    /// 使用给定的 <see cref="FileDistributedLock"/> 构造一个 <paramref name="name"/>.
    /// </summary>
    public FileDistributedLock CreateLock(string name) => new(this._lockFileDirectory, name);

    IDistributedLock IDistributedLockProvider.CreateLock(string name) => this.CreateLock(name);
}
